//
//  YWLocationInspectionRecordMoreTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 17/2/23.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWLocationInspectionRecordMoreTableViewController.h"
#import "YWInspecLocationRecordDetailTableViewController.h"
#import "YWNoQrcodeLedgerLocationRecordTableViewCell.h"

#import "UIColor+CustomColors.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "MJRefresh.h"
#import "YWMacro.h"
#import "YWCommonTool.h"
#import "NSDate+YYAdd.h"
#import "YWInspectionMissionManager.h"
#import "YWMissionManager.h"
#import "YWInspectionEquipmentRecord.h"

@interface YWLocationInspectionRecordMoreTableViewController (){
//    NSTimeInterval startTime;
}
@property (nonatomic, strong) NSMutableArray * dataSource;
@property (nonatomic, assign) NSInteger page;
@end

@implementation YWLocationInspectionRecordMoreTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"巡查记录";
    [self initTableView];
}

- (void)initTableView{
    self.page = 1;
    [self.tableView registerClass:[YWNoQrcodeLedgerLocationRecordTableViewCell class] forCellReuseIdentifier:YWNoQrcodeLedgerLocationRecordTableViewCellIdentifier];
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.sectionFooterHeight = 0.01f;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    [self.tableView.mj_header beginRefreshing];
}

- (void)refreshTop
{
    self.page = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType
{
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
    }
    YWWeakSelf(weakSelf);
//    if (weakSelf.dataSource.count>0) {
//        YWInspectionEquipmentRecord * inspectionRecord = [[weakSelf.dataSource lastObject] lastObject];
//        
//        startTime = inspectionRecord.executeTime;
//    }else{
//        startTime = 0;
//    }
    [[YWInspectionMissionManager shareManager] requestInspectionLocationRecord:self.locationId page:self.page pageSize:10 complete:^(BOOL success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
            else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            weakSelf.dataSource = nil;
            [weakSelf.tableView reloadData];
        });
    }];
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        NSArray * inspectionArray = [[YWMissionManager shareManager] keepWatchAndInspectRecordsInLedgerLocationDetailWithLocationId:self.locationId andPage:self.page type:YWKeepWatchOrInspectionTypeInspection];
        [_dataSource addObjectsFromArray:inspectionArray];
    }
    return _dataSource;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWNoQrcodeLedgerLocationRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWNoQrcodeLedgerLocationRecordTableViewCellIdentifier];
    cell.inspectionRecord = self.dataSource[indexPath.section][indexPath.row];
    return cell;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return [tableView fd_heightForCellWithIdentifier:YWNoQrcodeLedgerLocationRecordTableViewCellIdentifier cacheByIndexPath:indexPath configuration:^(YWNoQrcodeLedgerLocationRecordTableViewCell *cell) {
//        cell.inspectionRecord = self.dataSource[indexPath.section][indexPath.row];
//    }];
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWInspectionEquipmentRecord *inspectionRecord = self.dataSource[indexPath.section][indexPath.row];
    YWInspecLocationRecordDetailTableViewController * inspectionRecordDetailVC = [[YWInspecLocationRecordDetailTableViewController alloc] init];
    inspectionRecordDetailVC.inspectionEquipmentRecord = inspectionRecord;
    [self.navigationController pushViewController:inspectionRecordDetailVC animated:YES];
}

#pragma mark - table view section header view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString* HeaderIdentifier = @"YWEquipmentTroubleMoreTableViewControllerForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        label.tag = 2016;
        label.font = KTitleFont;
        [headerView addSubview:label];
    }
    
    NSString *headText = [self titleForHeaderInSection:section];
    UILabel* label = [headerView viewWithTag:2016];
    label.text = headText;
    label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 35);
    return headerView;
}

- (NSString *)titleForHeaderInSection:(NSInteger)section{
    NSArray *array = [self.dataSource[section] count]>0?self.dataSource[section]:nil;
    YWInspectionEquipmentRecord *equipmentRecord = array.count?array[0]:nil;
    NSString *time = [YWCommonTool showChineseFormatterDate:equipmentRecord.executeTime/1000];
    NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:equipmentRecord.executeTime/1000].weekday;
    NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
    NSString * title = [NSString stringWithFormat:@"%@  %@",time,weekStr];
    return title;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
